﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Windows.Forms;

namespace Tetris
{
    public partial class FrmTetris : Form
    {
        public FrmTetris()
        {
            InitializeComponent();
        }

        private Palette p;
        private Keys downKey; //下移键
        private Keys dropKey; //丢下键
        private Keys moveLeftKey; //左移键
        private Keys moveRightKey; //右移键
        private Keys deasilRotateKey; //顺时针旋转键
        private Keys contraRotateKey; //逆时针旋转键
        private int paletteWidth; //画板宽度
        private int paletteHeight; //画板高度
        private Color paletteColor; //背景色
        private int rectPix; //每个单元格像素
        private string bgSoundUrl = "Calypso.mid";
        private bool isPause = false;
        private bool isStart = false;

        private void pbRun_Paint(object sender, PaintEventArgs e)
        {
            if (p != null)
            {
                p.PaintPalette(e.Graphics);
            }
        }
        private void lblReady_Paint(object sender, PaintEventArgs e)
        {
            if (p != null)
            {
                p.PaintReady(e.Graphics);
            }
        }

        private void FrmTetris_Load(object sender, EventArgs e)
        {
            Config config = new Config();
            config.LoadFromXmlFile();
            downKey = config.DownKey;
            dropKey = config.DropKey;
            moveLeftKey = config.MoveLeftKey;
            moveRightKey = config.MoveRightKey;
            deasilRotateKey = config.DeasilRotateKey;
            contraRotateKey = config.ContraRotateKey;
            paletteWidth = config.CoorWidth;
            paletteHeight = config.CoorHeight;
            paletteColor = config.BackColor;
            rectPix = config.RectPix;
            //根据画板的宽高度信息动态改变窗体及画板大小
            this.Width = paletteWidth * rectPix + paletteWidth * 2 + 137;
            this.Height = paletteHeight * rectPix + 81;
            pbRun.Width = paletteWidth * rectPix;
            pbRun.Height = paletteHeight * rectPix;
            //快捷键显示
            lblLeft.Text = "左移：" + moveLeftKey;
            lblRight.Text = "右移：" + moveRightKey;
            lblDown.Text = "下移：" + downKey;
            lblDrop.Text = "丢下：" + dropKey;
            lblContra.Text = "左旋：" + contraRotateKey;
            lblDeasil.Text = "右旋：" + deasilRotateKey;
            axWMP.settings.autoStart = false;
        }

        private void FrmTetris_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyValue == 32) //屏蔽回车键
            {
                e.Handled = true;
            }
            if (e.KeyCode == downKey)
            {
                p.Down();
            }
            else if (e.KeyCode == dropKey)
            {
                p.Drop();
            }
            else if (e.KeyCode == moveLeftKey)
            {
                p.MoveLeft();
            }
            else if (e.KeyCode == moveRightKey)
            {
                p.MoveRight();
            }
            else if (e.KeyCode == deasilRotateKey)
            {
                p.DeasilRotate();
            }
            else if (e.KeyCode == contraRotateKey)
            {
                p.ContraRotate();
            }
        }

        private void FrmTetris_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (p != null)
            {
                p.Close();
            }
        }

        private void menuSatrt_Click(object sender, EventArgs e)
        {
            if (p != null)
            {
                p.Close();
            }
            if (menuSatrt.Text == "开始")
            {
                menuSatrt.Text = "重新开始";
            }
            p = new Palette(paletteWidth, paletteHeight, rectPix, paletteColor,
                Graphics.FromHwnd(pbRun.Handle),
                Graphics.FromHwnd(lblReady.Handle), this.tbScore);
            p.Start();
            isStart = true;
            axWMP.URL = bgSoundUrl;
            toPlay();
        }

        private void menuPause_Click(object sender, EventArgs e)
        {
            if (p == null)
            {
                return;
            }
            if (menuPause.Text == "暂停")
            {
                p.Pause();
                menuPause.Text = "继续";
                isPause = true;
            }
            else
            {
                p.EndPause();
                menuPause.Text = "暂停";
                isPause = false;
            }
            toPlay();
        }

        private void menuSetting_Click(object sender, EventArgs e)
        {
            if (menuPause.Text == "暂停")
            {
                menuPause.PerformClick();
            }
            using (FrmConfig frmConfig = new FrmConfig())
            {
                frmConfig.ShowDialog();
            }
        }

        private void menuExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void menuAbout_Click(object sender, EventArgs e)
        {
            if (menuPause.Text == "暂停")
            {
                menuPause.PerformClick();
            }
            using (FrmAbout frmAbout = new FrmAbout())
            {
                frmAbout.ShowDialog();
            }
        }

        private void axWMP_PlayStateChange(object sender, AxWMPLib._WMPOCXEvents_PlayStateChangeEvent e)
        {
            if (axWMP.playState == WMPLib.WMPPlayState.wmppsMediaEnded)
            {
                if (!isPause)
                {
                    axWMP.settings.setMode("loop", true);
                }
            }
        }
        private void toPlay()
        {
            if(isStart)
            {
                if (isPause)
                {
                    axWMP.Ctlcontrols.pause();
                }
                else
                {
                    axWMP.Ctlcontrols.play();
                }
            } 
        }  
    }
}
